package com.tql.view.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tql.view.pojo.Scenery;
import com.tql.view.pojo.User;
import com.tql.view.service.SceneryService;
import com.tql.view.util.FileDownloadUtil;
import com.tql.view.util.JsonUtil;
import org.apache.commons.collections.list.SynchronizedList;
import org.apache.commons.io.IOUtils;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;

@Controller
@RequestMapping("/scenery")
public class SceneryController {
    @Autowired
    private SceneryService sceneryService;
    @RequestMapping("/getAllScenery")
    public String getAllScenery(@RequestParam("mytype") String mytype, Model model, @RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "12") Integer size) {
        List<Scenery> list = sceneryService.getScenery(mytype,pageNum, size);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("mytype",mytype);
        return "view/view";
    }
    @RequestMapping("/getSceneryByPageView")
    public String getSceneryByPageView(Model model, @RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "12") Integer size) {
        List<Scenery> list = sceneryService.getSceneryByPageView(pageNum, size);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo", pageInfo);
        return "view/view1";
    }
    @RequestMapping("/getSceneryByDownLoad")
    public String getSceneryByDownLoad(Model model, @RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "12") Integer size) {

        List<Scenery> list = sceneryService.getSceneryByDownLoad(pageNum, size);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo", pageInfo);
        return "view/view2";
    }
    /*
    * 图片信息管理优化代码结构
    * */
    @RequestMapping("/getAllScenerys")
    public String getAllScenerys(Model model,
                                @RequestParam("mytype")String mytype,
                                @RequestParam(defaultValue = "1")Integer pageNum,
                                @RequestParam(defaultValue = "12") Integer size
                                ){
        List<Scenery> list=sceneryService.getScenery(mytype,pageNum,size);
        PageInfo pageInfo=new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("mytype",mytype);
        return "admin/getScenery";
    }
    //管理界面删除
    @RequestMapping("/deleteScenerys")
    public String deleteScenerys(@RequestParam("mytype") String mytype,@RequestParam("id")String id){
       sceneryService.deleteScenery(id);
        return "redirect:/scenery/getAllScenerys?mytype="+mytype;
    }
    //图片删除单机删除
    @RequestMapping("/deleteSceneryss")
    public String deleteSceneryss(@RequestParam("mytype") String mytype,@RequestParam("id")String id){
        sceneryService.deleteScenery(id);
        return "redirect:/scenery/getAllScenery?mytype="+mytype;
    }
    /*
    * 上传图片资源
    * */
   @RequestMapping("/insertScenery")
   public String insertScenery(@RequestParam("tablename")String tablename,
                               @RequestPart("face") MultipartFile face,
                               @RequestParam("name")String name,
                               @RequestParam("describe")String describe,
                               HttpSession session) {
       Map<String, Object> params = new HashMap<>();
       Date createTime = new Date();
       String id = UUID.randomUUID() + "tql";
       params.put("id", id);
       params.put("myname", name);
       params.put("describes", describe);
       params.put("createtime", createTime);
       params.put("mytype",tablename);
       params.put("pageview",0);
       params.put("download",0);
       //判断上传文件是否为空
       if (!face.isEmpty()) {
           try {

               //获取文件名称
               String originalFilename = face.getOriginalFilename();
               //随机生成图片后缀，UUID生成随机数+ '.jpg' 保证图片名唯一
               String finalFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
               face.transferTo(new File("E://view//"+tablename+"//" + finalFileName));
               String path = "/"+tablename+"/" + finalFileName;
               params.put("location", path);
           } catch (Exception e) {
               e.printStackTrace();
           }

       }
       sceneryService.insertScenery(params);
       return "redirect:/scenery/getAllScenerys?mytype="+tablename;

   }
    /*
     * 搜索框模糊查询
     * */
    @RequestMapping("/searchByName")
    public String searchByName(@RequestParam("myname")String myname,
                               @RequestParam(defaultValue = "1")Integer pageNum,
                               @RequestParam(defaultValue = "12") Integer size,
                               Model model){

        List<Scenery> list=sceneryService.searchByName(myname,pageNum,size);
        PageInfo pageInfo=new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("myname",myname);
        /*  List<Scenery> list3=sceneryService.searchByName("anime","喜欢多云的季节");*/
        return "view/search";
    }
  //根据图片链接下载图片
    @RequestMapping("/download")
    public String downloadFile(@RequestParam("location")String location,
                               @RequestParam("mytype") String mytype,
                               HttpServletResponse response,
                               HttpServletRequest request,
                               HttpSession session) throws InterruptedException {
        String sub= UUID.randomUUID()+"A";
        String url="http://localhost:8081"+location;
        String path1="E:/view/myimage/"+sub+".jpg";
        FileDownloadUtil.downloadPicture(url,path1);
        Thread.sleep(3000);
       return  "redirect:/scenery/getAllSceneryss?tablename="+mytype;
    }
    //签到
    @RequestMapping("/updateDownLoad")
    @ResponseBody
    public String updateDownLoad(HttpSession session,@RequestParam("id")String id){
        sceneryService.updateDownload(id);
        return JsonUtil.objectToJson("hello");
    }


}
